package abc.com.abc.activity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import abc.com.abc.R;
import abc.com.abc.bean.MSGBean;
import abc.com.abc.utils.AnalysisiUtils;
import abc.com.abc.utils.DBUtils;
import abc.com.abc.utils.SQLiteHelper;

import static abc.com.abc.utils.DBUtils.db;

public class MessageActivity  {

    private Activity mContext;
    private LayoutInflater mInflater;
    private View mCurrentView;
    private TextView message_show;//最新信息显示
    private EditText et_Recipient,et_note;//接收者和发送的消息输入框
    private Button bt_send;//发送
    private String spUserName;
    public MessageActivity (Activity context) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        spUserName = AnalysisiUtils.readLoginUserName(mContext);
    }

    private void createView() {

        initView();

    }
    private void initView() {
        mCurrentView = mInflater.inflate(R.layout.activity_message, null);
        mCurrentView.setVisibility(View.VISIBLE);
        message_show = mCurrentView.findViewById(R.id.message_show);
        et_Recipient = mCurrentView.findViewById(R.id.et_Recipient);
        et_note = mCurrentView.findViewById(R.id.et_note);
        bt_send = mCurrentView.findViewById(R.id.Send);
        bt_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String receiver = et_Recipient.getText().toString();//接收者
                String msg = et_note.getText().toString();//信息
                if (TextUtils.isEmpty(msg)||(TextUtils.isEmpty(receiver))){
                    Toast.makeText(mContext, "发送人可以输入教练名驾校,   并且发送信息不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    ContentValues values = new ContentValues();
                    //开始添加第一条数据
                    values.put("userName",spUserName);
                    values.put("receiver",receiver);
                    values.put("msg",msg);

                    MSGBean bean = new MSGBean();
                    bean.msg = msg;
                    bean.receiver = receiver;
                    bean.userName = spUserName;
                    DBUtils.getInstance(mContext).saveMSG(bean);//插入第一条数据

                    message_show.setText("发送成功信息显示   发送给" +
                            values.get("receiver") + ",内容是" + values.get("msg")+"  发送人："
                    +values.get("userName"));
                    et_Recipient.setText("");
                    et_note.setText("");
                    Toast.makeText(mContext, "发送成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    /**
     * 获取当前z导航栏上方显示的View
     */
    public View getView(){
        if (mCurrentView == null){
            createView();
        }
        return mCurrentView;
    }

    /**
     * 显示当前导航栏上方所对应的VIEW界面
     */
    public  void showView(){
        if (mCurrentView == null){
            createView();
        }
        mCurrentView.setVisibility(View.VISIBLE);
    }
}
